package com.yzd.learnjava.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.yzd.learnjava.util.Print;

public class SimplePriority implements Runnable{
	private int countdown = 5;
	private volatile double d ;
	private int priority;
	public SimplePriority(int priority) {
		this.priority = priority;
	}
	
	public String toString() {
		return Thread.currentThread() + ": " + countdown;
	}
	
	public void run() {
		Thread.currentThread().setPriority(priority);
		while(true) {
			for(int i = 1 ; i < 100000; i++) {
				d += (Math.PI + Math.E) / (double)i;
				if(i % 1000 == 0) {
					Thread.yield();
				}
			}
			Print.print(this);
			if(--countdown == 0) {
				return;
			}
		}
	}
	
	public static void main(String[] args) {
		ExecutorService exec = Executors.newCachedThreadPool();
		for(int i = 0 ; i < 5 ; i++) {
			exec.execute(new SimplePriority(Thread.MIN_PRIORITY));
		}
		exec.execute(new SimplePriority(Thread.MAX_PRIORITY));
		exec.shutdown();
	}

}
